﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kooboo.IoC;
using Kooboo.Commerce.Promotions.DiscountByBrand.Models;
using Kooboo.Web.Mvc.Paging;
using Kooboo.Globalization;

namespace Kooboo.Commerce.Promotions.DiscountByBrand.Controllers
{
    [Authorize(Roles = "Administrator")]
    public class DiscountByBrandController : Controller
    {
        //
        // GET: /DiscountByBrand/
        public ActionResult Index(int id)
        {
            int? selectedReduceType = null;
            int? selectedBrand = null;
            var rule = Services.Current.Find(id);
            if (rule == null)
            {
                rule = new Promotion_DiscountByBrand
                {
                    DiscountRuleId = id
                };
            }
            else
            {
                selectedReduceType = rule.ReduceType;
                selectedBrand = rule.BrandId;
            }
            if (selectedBrand.HasValue)
            {
                ViewData["SelectedBrandName"] = ContextContainer.Current.Resolve<IBrandRepository>().Brands.Where(b => b.Id == selectedBrand.Value).First().Name;
                ViewData["SelectedBrand"] = selectedBrand.Value;
            }
            else
            {
                ViewData["SelectedBrandName"] = "Not Selected".Localize();
                ViewData["SelectedBrand"] = null;
            }

            ViewData["ReduceType"] = EnumHelper.ConvertEnumToSelectListItem<ReduceType>(selectedReduceType);
            ViewData["SearchBrandAction"] = Url.Action("SearchBrand");
            return View(rule);
        }

        [HttpPost]
        public ActionResult Index(Promotion_DiscountByBrand promotion, String next)
        {
            if (next == "prev")
            {
                return Json(new { Data = this.Url.Action("Edit", "PromotionRule", new { Id = promotion.DiscountRuleId }) });
            }
            else
            {
                if (promotion.DiscountRuleId > 1)
                {
                    if (promotion.ReduceType == (int)ReduceType.ByFee && promotion.ReduceValue <= 0)
                    {
                        return Json(new { Error = "Please enter a valid discount fee" });
                    }

                    if (promotion.ReduceType == (int)ReduceType.ByPercent && (promotion.ReduceValue < 0 || promotion.ReduceValue > 1))
                    {
                        return Json(new { Error = "Please enter a valid discount fee" });
                    }

                    if (promotion.BrandId < 1)
                    {
                        return Json(new { Error = "Please choose a brand" });
                    }

                    var item = Services.Current.DataContext.Promotion_DiscountByBrands.Where(d => d.DiscountRuleId == promotion.DiscountRuleId).FirstOrDefault();
                    if (item == null)
                    {
                        item = new Promotion_DiscountByBrand()
                        {
                            DiscountRuleId = promotion.DiscountRuleId
                        };
                        Services.Current.DataContext.Promotion_DiscountByBrands.InsertOnSubmit(item);
                    }

                    item.BrandId = promotion.BrandId;
                    item.ReduceType = promotion.ReduceType;
                    item.ReduceValue = promotion.ReduceValue;

                    Services.Current.DataContext.SubmitChanges();
                }

                return Json(new { Data = this.Url.Action("Index", "PromotionRule") });
            }
        }

        public ActionResult SearchBrand(Int32? page)
        {
            int selectedBrandId = 0;
            Int32.TryParse(Request["BrandId"], out selectedBrandId);

            var brandContext = ContextContainer.Current.Resolve<IBrandRepository>();
            var brands = brandContext.Brands.ToPagedList(page ?? 1, Configuration.PageSize);
            return Json(new
            {
                Items = brands.Select(b => new { b.Id, b.Name, Selected = (selectedBrandId > 0) ? selectedBrandId == b.Id : false }).ToArray(),
                Total = brands.TotalItemCount,
                PageCount = brands.TotalPageCount,
                PageSize = brands.PageSize,
                PageIndex = brands.CurrentPageIndex
            }, JsonRequestBehavior.AllowGet);
        }
    }
}
